// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import { game } from "../../../../script/game/control/game";
import { ccs } from "../../../../script/util/services";
import { spriteClass } from "../../../../script/util/SpritExd";
import { WinBase } from "../../../../script/wins/WinBase";

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends WinBase {

    @property(cc.Label)
    label: cc.Label = null;

    minMoney: number = 20000;
    money: number = 10000;
    count: number = 6; //交换次数
    canChoose: boolean = false; //是否可以选
    pokerArr: number[] = [1, 2, 3];
    multiple: number = 1; //下注的倍数
    speed: number = 0.4;
    times: number = 6;
    size: number = 0;
    start () {
        this.addBtn();
        this.money = this._args.money;
    }
    
    addBtn() {
            this.nodes.note.getComponent(cc.Label).string = `请注入期望值`;
            game.playerMgr.calMoney(-this.money);
            //生成号数
            let i = 0;
            while(i < 3) {
                i++;
                this.pokerArr.push(ccs.gf.randomInt(1, 6));
            }
            this.touchEndEventScale(this.nodes.big, () => {
                this.size = 1;
                this.fadeOut();
                this.nodes.note.getComponent(cc.Label).string = `当前期望为大`;
            })
            this.touchEndEventScale(this.nodes.small, () => {
                this.size = 0;
                this.fadeOut();
                this.nodes.note.getComponent(cc.Label).string = `当前期望为小`;
            })
    }
    
    fadeOut() {
        cc.tween(this.nodes.big)
        .by(0.5, {x: -200, opacity: 0})
        .start();
        cc.tween(this.nodes.small)
        .by(0.5, {x: 200, opacity: 0})
        .call(() => {
            this.nodes.size.active = false;
            this.nodes.ani.active = true;
            this.getGamble();
        })
        .start();
    }
    getGamble() {
        this.nodes.ani.children.forEach( val => {
            this.startAni(val);
        })
        this.scheduleOnce(() => {
            this.nodes.ani.getComponent(cc.Layout).type = null;   
            this.changePosition();
        }, 2.5);
    }
    startAni(node: cc.Node) {
        //先缩小，再放大
        let tween = cc.tween()
        .to(1, {opacity: 0, scale: 0, angle: 360})
        let trunOut = cc.tween()
        .to(1, {angle: 0, opacity: 255, scale: 1});
        cc.tween(node).then(tween).then(trunOut).start();
    }
    changePosition() {
        // 几次
        let count = 0;
        while(count < 6) {
            this.nodes.ani.children.forEach(val => {
                this.scheduleOnce(() => {
                    ccs.spriteExd.setSprite(val, ccs.resMgr.getGambleIcon(ccs.gf.randomInt(1, 6)));
                }, 0.5 * count);
            })
            count ++;
        }
        this.nodes.ani.children.forEach((val, i) => {
            this.scheduleOnce(() => {
                ccs.spriteExd.setSprite(val, ccs.resMgr.getGambleIcon(this.pokerArr[i]));
            }, 2);
        })
        this.scheduleOnce(() => {
            this.openFun();
        }, 3);
        
    }
    openFun() {
        let size = 0;
        this.pokerArr.forEach(val => size += val);
        if((size <= 11 && this.size == 0) || (size > 11 && this.size == 1)) {
            this.nodes.result.getComponent(cc.Label).string = `点数为${size},恭喜你赢了，获得${this.money}的奖励`;
            game.recordMgr.addList({name: "你", action: `获得${this.money}的奖励`,effect: `金钱 + ${this.money}元`, location: "东华娱乐城，骰子术pk中，运气爆炸"});
            game.playerMgr.calMoney(this.money * 2);
        } else {
            this.nodes.result.getComponent(cc.Label).string = `点数为${size},不好意思，你的游戏输了，共${this.money}元`;
            game.recordMgr.addList({name: "你", action: `输的叮当响`,effect: `金钱 - ${this.money}元`, location: "东华娱乐城，骰子术pk中，偶有失利"});

        }
        this.scheduleOnce(() => {
            if(game.playerMgr.heroInfo.money <= 0) {
                this._close();
            } else {
                this._close();
                ccs.winMgr.showWin("gambleGuessWin", {type: "size"});
            }
        }, 2)
    }
    // update (dt) {}
}
